//
//  MyMessageViewController.m
//  囧途
//
//  Created by fly on 16/3/3.
//  Copyright © 2016年 fly. All rights reserved.
//

#import "MyMessageViewController.h"
#import "UIImageView+WebCache.h"
#import "PhoneNumberViewController.h"
#import <AVUser.h>
#import "ControllerManger.h"
#import "ResignViewController.h"
#import "PersonInfoViewController.h"
#import "LoginViewController.h"
#import <AVObject.h>

#import <AVFile.h>

@interface MyMessageViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *headImage;
@property (strong, nonatomic) IBOutlet UIButton *phoneNumber;
@property (strong, nonatomic) IBOutlet UILabel *userName;
@property (strong, nonatomic) IBOutlet UITextField *telPhone;
@property(nonatomic, strong) AVUser * currentUser;
@property(nonatomic, strong)UIBarButtonItem * rightButton;


@end

@implementation MyMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = self.rightButton;
    
    
   }

-(void)viewWillAppear:(BOOL)animated
{
    _currentUser = [AVUser currentUser];
    if (_currentUser != nil) {
        
        AVObject * userObj = [AVObject objectWithoutDataWithClassName:@"_User" objectId:_currentUser.objectId];
    
        [userObj fetchInBackgroundWithBlock:^(AVObject *object, NSError *error) {
           
            _userName.text = userObj[@"localData"][@"NameLabel"];
            
            _telPhone.text = userObj[@"localData"][@"TurePhoneNumber"];
        }];
        
        
    } else {
        
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"你还没有登录哦！" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction * alertAction = [UIAlertAction actionWithTitle:@"去注册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            ResignViewController * view = [[ResignViewController alloc]init];
            
            [self.navigationController pushViewController:view animated:YES];
            
        }];
        UIAlertAction * alertAction1 = [UIAlertAction actionWithTitle:@"去登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            LoginViewController * view = [[LoginViewController alloc]init];
            
            [self.navigationController pushViewController:view animated:YES];
            
            
        }];

        [alert addAction:alertAction];
        
        [alert addAction:alertAction1];
        
        [self presentViewController:alert animated:YES completion:nil];
        
        
    }


}

- (IBAction)phoneNumberPressed:(UIButton *)sender {
    
    PhoneNumberViewController * view = [[PhoneNumberViewController alloc]init];
    
    [self.navigationController pushViewController:view animated:YES];
    
    
}

-(UIBarButtonItem *)rightButton
{
    if (!_rightButton) {
        
        _rightButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(rightButtonPressed:)];
        
    }
    return _rightButton;
}

-(void)rightButtonPressed:(UIBarButtonItem *)sender
{
    PersonInfoViewController * person = [[PersonInfoViewController alloc]init];
    
    [self.navigationController pushViewController:person animated:YES];

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
